import axios from "axios";
import { error } from "console";
import { ElMessage } from "element-plus";


//自定义axios
const request = axios.create({
    baseURL:'http://localhost:8080'
})



//添加请求拦截器
request.interceptors.request.use(
    //请求前的回调
    (config)=>{
        const token = localStorage.getItem('token')
        if(token){
            //在请求头 添加token
            config.headers.Authorization = token
        }

        return config
    }
)

import router from "../router";

//添加响应拦截器
request.interceptors.response.use(
   result => {
        return result
   } ,
   error=>{
    
    if(error.response.status === 401){
        ElMessage.error('请先登录')
        router.push('/login')
    }else{
        ElMessage.error('服务器异常')
    }

    //将异步状态转为失败
    Promise.reject(error)

   }
)




export default request